layui.use(['form', 'layer', 'jquery'], function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;


    $(".loginBody .seraph").click(function() {
            layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧", {
                time: 5000
            });
        })
        // 清除字符串两边的空格
    function trim(str) { //删除左右两端的空格
        return str.replace(/(^\s*)|(\s*$)/g, "");　　
    }
    //登录按钮
    form.on("submit(login)", function(data) {

        var username = trim(document.getElementById("userName").value);
        var password = trim(document.getElementById("password").value);


        var res = verifyCode.validate(document.getElementById("code_input").value);
        if (res) {
            console.log("验证正确");
        } else {
            layer.msg("验证码错误", {
                time: 2000
            });
            sign = false;
            return false;
        }

        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        var sign = true;
        if (username == "") {
            layer.msg("用户名不能为空", {
                time: 2000
            });
            sign = false;
            return false;

        }

        if (!myreg.test(username)) {
            layer.msg("请输入有效的手机号码", {
                time: 2000
            });
            sign = false;
            return false;
        }

        if (password == '') {
            layer.msg("密码不能为空", {
                time: 2000
            });
            sign = false;
            return false;
        }

        console.log(password)

        // return

        if (sign) {
            console.log(severUrl)
            $.post(severUrl + "manage_login", {
                'tel': username,
                'pwd': password
            }, function(data) {
                console.log(data)
                if (data.data.message == '密码错误' || data.data.message == '用户名错误') {
                    layer.msg(data.data.message, {
                        time: 5000
                    });
                } else if (data.data.message == '登录成功') {

                    window.sessionStorage.setItem("user_name", username);
                    window.location.href = "../../index.html";
                }

            })
        }
        return false;
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function() {
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function() {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    })
})